#!/bin/env python

from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm


env.hosts = ['worker@115.29.187.198']

from fabric.api import *

def host_type():
    run('uname -s')

def sync_mysql():
	local("mysqldump -h 127.0.0.1 -P8888 -u root -proot --add-drop-database --add-drop-table  --default-character-set=utf8 --database dev_game > game.sql")
	local("tar -jcf game.sql.tar.bz2 game.sql")
	local("scp game.sql.tar.bz2 %s:~/"%(env.hosts[0]))
	run("tar -jxf ~/game.sql.tar.bz2")
	run("mysql -h 127.0.0.1 -uroot -proot < ~/game.sql")

def start_redis():
	run("sh /home/worker/output/script/load_redis_script.sh")


def online():
	build_root = "/home/work/workspace/online/trunk"
	with lcd(build_root):
		local("svn up")
		with lcd("build"):
			local("rm -rf *")
			local("cmake ..")
			if(local("make -j 4").succeeded):
				local("make install")
				local("echo 'work' | sudo -S supervisorctl restart all")
				local("tar -jcf output.tar.bz2 output")
				local("scp -r output.tar.bz2  %s:~/"%(env.hosts[0]))
				pass
	run("rm -rf ~/output")
	run("tar -jxf output.tar.bz2")
	run("sh /home/worker/output/script/load_redis_script.sh")
	run("supervisorctl restart all")
	#run("ps -ef | grep -E 'router|simple_worker|gateway|small_map_worker' | grep -v 'grep' |awk '{print $2}' | xargs -i -t kill -9 {}")
	#run("/home/worker/output/script/start.sh" , shell=True, pty=False)
	#run("export LD_LIBRARY_PATH=/home/worker/output/lib;export GameLog=/home/worker/output/conf/game.conf;nohup /home/worker/output/bin/gateway >> /tmp/gateway.log 2>&1  &", shell=True, pty=False )
	#run("export LD_LIBRARY_PATH=/home/worker/output/lib;export GameLog=/home/worker/output/conf/game.conf;/home/worker/output/bin/router broker01 "tcp://*:5555" -v >> /tmp/router.log 2>&1 &")
	#run("export LD_LIBRARY_PATH=/home/worker/output/lib;export GameLog=/home/worker/output/conf/game.conf;/home/worker/output/bin/simple_worker worker01 "tcp://localhost:5555" "echo" -v >> /tmp/simple_worker.log 2>&1 &")
	#run("export LD_LIBRARY_PATH=/home/worker/output/lib;export GameLog=/home/worker/output/conf/game.conf;/home/worker/output/bin/small_map_worker > /tmp/small_map.log 2>&1 &")


def get_online_corefile():
	run("tar -jcf /home/worker/online_core.tar.bz2 /home/worker/core-*")
	local("scp %s:~/online_core.tar.bz2 /home/work/workspace/online_core_file/"%(env.hosts[0]))



